package com.yjj.demo.thread.service;

import com.yjj.demo.thread.context.BeanContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 *
 *
 * @author yangjiajia
 * @time 2021/8/22 20:15
 */
@Service
public class NotifyService {

    private Logger logger = LoggerFactory.getLogger(NotifyService.class);

    @Autowired
    private MailService mailService;

    public void send(String msg) {
        logger.info("消息通知,msg:{}", msg);

        //发送邮件
        sendMail(msg);

        logger.info("消息通知结束");
    }

    public void send() {
        String msg = UUID.randomUUID().toString();
        for (int i = 0; i < 10; i ++) {
            mailService.send1(msg);
        }

        logger.info("发送成功");
    }

    private void sendMail(String msg) {
        new MailThread(msg).start();
    }

    static class MailThread extends Thread {

        private Logger logger = LoggerFactory.getLogger(MailThread.class);

        private String msg;

        private MailThread() {}
        private MailThread(String msg) {
            this.msg = msg;
        }

        @Override
        public void run() {
            MailService mailService = BeanContext.getBean(MailService.class);
            mailService.send(msg);
        }
    }
}
